﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class TriggerEvent : MonoBehaviour
{
    public string targetTag = "Player";
    public UnityEvent onEnter;
    public UnityEvent<Collider> onEnterDynamic;
    public UnityEvent onExit;
    public UnityEvent<Collider> onExitDynamic;
    public Collider stayCollider;
    bool isStay = false;
    bool last = false;

    private void OnEnable()
    {
        isStay = false;
        last = false;
    }

    private void OnTriggerStay(Collider other)
    {
        if (enabled)
        {
            stayCollider = other;
            isStay = true;
            //Debug.Log(stayCollider.gameObject.name);
        }
    }

    private void FixedUpdate()
    {
        if (isStay && !last)
        {
            onEnter?.Invoke();
            onEnterDynamic?.Invoke(stayCollider);
        }
        if (last && !isStay)
        {
            onExit?.Invoke();
            onExitDynamic?.Invoke(stayCollider);
        }
        last = isStay;
        isStay = false;
        stayCollider = null;
    }

    private void OnDisable()
    {
        onExit?.Invoke();
    }
}
